<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1110030
-->
<head>
  <title>Forwarding Hardware Key to InputMethod</title>
  <script type="application/javascript;version=1.7" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript;version=1.7" src="inputmethod_common.js"></script>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/NativeKeyCodes.js"></script>
  <script type="text/javascript" src="bug1110030_helper.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1110030">Mozilla Bug 1110030</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">
// The input context.
var gContext = null;

// The test cases.
var gTests;

inputmethod_setup(function() {
  setInputContext();
});

function setInputContext() {
  let im = navigator.mozInputMethod;

  im.oninputcontextchange = function() {
    ok(true, 'inputcontextchange event was fired.');
    im.oninputcontextchange = null;

    gContext = im.inputcontext;
    if (!gContext || !gContext.hardwareinput) {
      ok(false, 'Should have a non-null inputcontext.hardwareinput');
      inputmethod_cleanup();
      return;
    }

    prepareTest();
  };

  // Set current page as an input method.
  SpecialPowers.wrap(im).setActive(true);

  // verifyResultsAndMoveNext will be called after input#text-input
  // receives all expected key events and it will verify results
  // and start next test.
  loadTestFrame(verifyResultsAndMoveNext);
}

function prepareTest()
{
  // Set the used input method of this test
  gInputMethod = new InputMethod(gContext);

  // Add listenr to hardwareinput
  addKeyEventListeners(gContext.hardwareinput, function (evt) {
    hardwareEventReceiver(evt);
    gInputMethod.handler(evt);
  });

  // Set the test cases
  gTests = [
    // Case 1: IME handle the key input
    {
      key: 'z',
      hardwareinput: {
        expectedEvents: kKeyDown | kKeyUp,
        receivedEvents: 0,
        expectedKeys: 'zz', // One for keydown, the other for keyup
        receivedKeys: '',
      },
      inputtext: {
        expectedEvents: kKeyDown | kKeyPress | kKeyUp,
        receivedEvents: 0,
        expectedKeys: gInputMethod.mapKey('z') +  // for keydown
                      gInputMethod.mapKey('z') +  // for keypress
                      gInputMethod.mapKey('z'),   // for keyup
        receivedKeys: '',
      }
    },
    // case 2: IME doesn't handle the key input
    {
      key: '7',
      hardwareinput: {
        expectedEvents: kKeyDown | kKeyUp,
        receivedEvents: 0,
        expectedKeys: '77', // One for keydown, the other for keyup
        receivedKeys: '',
      },
      inputtext: {
        expectedEvents: kKeyDown | kKeyPress | kKeyUp,
        receivedEvents: 0,
        expectedKeys: '777', // keydown, keypress, keyup all will receive key
        receivedKeys: '',
      }
    },
    // case 3: IME is disable
    // This case is same as
    // dom/events/test/test_dom_before_after_keyboard_event*.html
  ];

  startTesting();
}

function startTesting()
{
  if (gTests.length <= 0) {
    finish();
    return;
  }

  gCurrentTest = gTests.shift();

  fireEvent();
}

function verifyResultsAndMoveNext()
{
  verifyResults(gCurrentTest);
  startTesting();
}

function finish()
{
  inputmethod_cleanup();
}

function errorHandler(msg)
{
  // Clear the test cases
  if (gTests) {
    gTests = [];
  }

  ok(false, msg);

  inputmethod_cleanup();
}

</script>
</pre>
</body>
</html>
